//
//  TwobarcodeViewController.m
//  XLZChat
//
//  Created by dllo on 16/10/24.
//  Copyright © 2016年 徐昊. All rights reserved.
//  二维码

#import "TwobarcodeViewController.h"
#import "Masonry.h"
@interface TwobarcodeViewController ()

/** 二维码 */
@property (nonatomic, strong) UIImageView *ImageView2D;
/** 头像 */
@property (nonatomic, strong) UIImageView *imagePhoto;
/** 网名 */
@property (nonatomic, strong) UILabel *labelName;

@end

@implementation TwobarcodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的二维码";
   

    [self creatImageView];
    [self creatSubView];
    [self creatLogoImage];
}
// 创建二维码相框
- (void)creatImageView {
    self.ImageView2D = [[UIImageView alloc] init];
    _ImageView2D.backgroundColor = [UIColor redColor];
    [self.view addSubview:_ImageView2D];
    [_ImageView2D mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.centerY.mas_equalTo(self.view.mas_centerY);
        make.width.mas_equalTo(self.view.bounds.size.width / 4 * 3);
        make.height.mas_equalTo(self.view.bounds.size.width / 4 * 3);
    }];
}

- (void)creatSubView {
    // 网名
    self.labelName = [[UILabel alloc] init];
     _labelName.text = @"二维码";
    _labelName.textColor = [UIColor blackColor];
    _labelName.backgroundColor = [UIColor blueColor];
    _labelName.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:_labelName];
    [_labelName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_ImageView2D.mas_bottom);
        make.left.mas_equalTo(_ImageView2D.mas_left);
        make.width.mas_equalTo(_ImageView2D.frame.size.width / 3 * 2);
        make.height.mas_equalTo(50);
    }];
    // 头像
    self.imagePhoto = [[UIImageView alloc] init];
    _imagePhoto.backgroundColor = [UIColor yellowColor];
    _imagePhoto.image = [UIImage imageNamed:@"头像"];
    [self.view addSubview:_imagePhoto];
    [_imagePhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_ImageView2D.mas_right);
        make.top.mas_equalTo(_ImageView2D.mas_bottom);
        make.width.mas_equalTo(_ImageView2D.frame.size.width / 3);
        make.height.mas_equalTo(_labelName.mas_height);
    }];
}
// 生成二维码
- (void)creatLogoImage {
    // 二维码过滤器
    CIFilter *filterImage = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filterImage setDefaults];
    NSData *dataImage = [_labelName.text dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"%@", filterImage.inputKeys);
    [filterImage setValue:dataImage forKey:@"inputMessage"];
    CIImage *outPutImage = [filterImage outputImage];
    outPutImage = [outPutImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
    UIImage *image = [UIImage imageWithCIImage:outPutImage];
    _ImageView2D.image = image;
    // 开启绘图, 获取图片上下文
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    CGFloat width = self.view.bounds.size.width / 4 * 3;
    CGFloat height = width;
    [_imagePhoto.image drawInRect:CGRectMake((image.size.width - width / 4) / 2, (image.size.height - height / 4) / 2, width / 4, height / 4)];
    // 获取最重图片
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭上下文
    UIGraphicsGetCurrentContext();
    _ImageView2D.image = finalImage;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
